package com.websocket.Controller;

import com.websocket.config.WebSocketServer.WebSocket;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName WebSocketController
 * @Date 2021/6/30 0030 下午 13:38
 * @Author Administrator
 * @Description TODO
 * Version 1.0
 */
@RestController
@RequestMapping("/socket")
public class WebSocketController {
    @Autowired
    private WebSocket webSocket;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendSocket")
    public void sendMessage(@RequestParam("message") String message) throws IOException {
        webSocket.sendMessageAll(message);
    }


    @RequestMapping("/senMQ")
    public String sendMQ(String msg) {
        String routeKey = "";
        String exchangeName = "htmlPush";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd HH时mm分ss秒");
        String dateString = simpleDateFormat.format(new Date());
        msg = msg +" ==> "+ dateString;
        rabbitTemplate.convertAndSend(exchangeName, routeKey, msg);
        System.err.println("rabbitmq发送完毕，消息为："+msg);
        return msg;
    }
}
